package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.TypedValue;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.fondesa.recyclerviewdivider.DividerDecoration;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.utils.DefaultUtil;
import com.sgb.kjwl.view.widget.transport.ImagePreviewDialog;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 杨畔
 * @date: 3/1/21 9:40 AM
 * @Description: 车型图弹窗
 */
public class CarTypePicPopWindow extends BasePopWindow {

    private PicAdapter mAdapter;
    private List<GoodsInfoTypeEntity3> mList;

    public CarTypePicPopWindow(Context context) {
        super(context);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_car_type_pic;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        RecyclerView recyclerView = holder.getPop().findViewById(R.id.rv_list);
        mList = new ArrayList<>();
        mAdapter = new PicAdapter(mList);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        DividerDecoration.builder(mContext)
                .color(Color.TRANSPARENT)
                .size(10, TypedValue.COMPLEX_UNIT_DIP)
                .build()
                .addTo(recyclerView);
        mAdapter.bindToRecyclerView(recyclerView);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            //查看大图
            ImagePreviewDialog.newInstance(mList.get(position).getTypeImg()).show(mContext);
        });
        //设置背景
        setBgView(holder.getPop().findViewById(R.id.bg_pop_gray_layout));
    }

    public void setData(List<GoodsInfoTypeEntity3> list) {
        mList.clear();
        if (list != null) {
            mList.addAll(list);
        }
        //删除不限
        if (mList.size() > 0 && "不限".equals(mList.get(0).getName())) {
            mList.remove(0);
        }
        mAdapter.notifyDataSetChanged();
    }

    public void setDefItem(int position) {
        mAdapter.setSelectIndex(position);
        mAdapter.notifyDataSetChanged();
    }

    private static class PicAdapter extends BaseQuickAdapter<GoodsInfoTypeEntity3, BaseViewHolder> {
        //选中下标
        private int mSelectIndex = -1;

        public PicAdapter(@Nullable List<GoodsInfoTypeEntity3> data) {
            super(R.layout.item_car_type_pic, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, GoodsInfoTypeEntity3 item) {
            helper.setText(R.id.tv_item_car_type_pic, DefaultUtil.getString(item.getName()))
                    .setVisible(R.id.v_item_car_type_pic_select, helper.getLayoutPosition() == mSelectIndex);
            //设置图片
            if (!TextUtils.isEmpty(item.getTypeImg())) {
                Glide.with(mContext).load(item.getTypeImg()).into((ImageView) helper.getView(R.id.iv_item_car_type_pic));
            } else {
                helper.setImageResource(R.id.iv_item_car_type_pic, R.color.white);
            }
        }

        public void setSelectIndex(int mSelectIndex) {
            this.mSelectIndex = mSelectIndex;
        }
    }
}
